package com.hemaapp.yjnh.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.yjnh.adapter.RankingAdapter;
import com.hemaapp.yjnh.bean.Family;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;

/**家族排名
 * @author ZhangGuiyou
 * Date: 2015-8-4 12:00
 */
public class RankingActivity extends BaseActivity {

	private ImageButton titleLeft;
	private TextView titleText;
	
	private XtomRefreshLoadmoreLayout layout;
	private XtomListView listView;
	
	private ArrayList<Family> families = new ArrayList<Family>();
	private User user;
	private RankingAdapter adapter;
	private int page;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_family_rank);
		user = getApplicationContext().getUser();
		super.onCreate(savedInstanceState);
		getRankList();
	}
	
	private void getRankList() {
		if (user != null) {
			getNetWorker().familyList(user.getToken(), page+"");
		}
	}
	
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case FAMILY_LIST:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case FAMILY_LIST:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case FAMILY_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<Family> cResult = (HemaPageArrayResult<Family>) baseResult;
			if (cResult == null || cResult.getObjects() == null)
				return;
			if ("0".equals(page)) {// 刷新
				layout.refreshSuccess();
				families.clear();
				families.addAll(cResult.getObjects());
			}else {
				layout.loadmoreSuccess();
				if (cResult.getObjects().size() > 0)
					families.addAll(cResult.getObjects());
				else {
					layout.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshData(-1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case FAMILY_LIST:
			showTextDialog(baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case FAMILY_LIST:
			refreshData(failedType);
			break;
		default:
			break;
		}
	}
	
	private void refreshData(int type) {
//		sortDatas();//先重新进行排序
		if (adapter == null) {
			adapter = new RankingAdapter(mContext, families, listView);
			adapter.setEmptyString("暂无排名");
			listView.setAdapter(adapter);
		}else {
			adapter.setEmptyString("暂无排名");
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(type);
	}
	
	/**对数据进行排名
	 * 
	 */
	private void sortDatas() {
		Collections.sort(families, new Comparator<Family>() {

			@Override
			public int compare(Family lhs, Family rhs) {
				//从大到小排列
				String score1 = lhs.getscore();
				String score2 = rhs.getscore();
				Double a = Double.parseDouble(lhs.getscore());
				Double b = Double.parseDouble(rhs.getscore());
				return (int) (b-a);
			}
		});
	}

	@Override
	protected void findView() {
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleText = (TextView) findViewById(R.id.title_text);
		
		layout = (XtomRefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listView = (XtomListView) findViewById(R.id.listview);
	}

	@Override
	protected void getExras() {
		
	}

	@Override
	protected void setListener() {
		titleLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleText.setText("佣金排名");
		layout.setOnStartListener(new StartListener());
		listView.setDividerHeight(0);
	}

	private class StartListener implements OnStartListener {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
			page = 0;
			getRankList();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
			page++;
			getRankList();
		}
	}
}
